import Flutter
import UIKit

public class SwiftAifPhotoPlugin: NSObject, FlutterPlugin {
  public static func register(with registrar: FlutterPluginRegistrar) {
    let channel = FlutterMethodChannel(name: "aif_photo_plugin", binaryMessenger: registrar.messenger())
    let instance = SwiftAifPhotoPlugin()
    registrar.addMethodCallDelegate(instance, channel: channel)
  }

  public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
    switch call.method {
    case "getPlatformVersion":getPlatformVersion(call, result: result)
    case "getAllAlbums":getAllAlbums(call, result: result)
    case "getSmartAlbums":getSmartAlbums(call, result: result)
    case "getUserAlbums":getUserAlbums(call, result: result)
    case "getAlbum":getAlbum(call, result: result)
    case "getAllAssets":getAllAssets(call, result: result)
    case "getAlbumAssets":getAlbumAssets(call, result: result)
    case "getAsset":getAsset(call, result: result)
    case "getAssetPath":getAssetPath(call, result: result)
    case "getAssetThumb":getAssetThumb(call, result: result)
    default:break
    }
  }
  
  public func getPlatformVersion(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
    result("iOS " + UIDevice.current.systemVersion)
  }
  
  /// 获取所有相册
  /// - Parameters:
  ///   - call: call
  ///   - result: result
  public func getAllAlbums(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
    let smartAlbums = AIPhotoTool.fetchSmartAlbums()
    let userAlbums = AIPhotoTool.fetchUserAlbums()
    var allJsonList = smartAlbums.toAifJson()
    allJsonList = allJsonList + userAlbums.toAifJson()
    result(allJsonList)
  }
  /// 获取智能相册
  /// - Parameters:
  ///   - call: call
  ///   - result: result
  public func getSmartAlbums(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
    let smartResult = AIPhotoTool.fetchSmartAlbums()
    result(smartResult.toAifJson())
  }
  /// 获取用户相册
  /// - Parameters:
  ///   - call: call
  ///   - result: result
  public func getUserAlbums(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
    let userAlbums = AIPhotoTool.fetchUserAlbums()
    result(userAlbums.toAifJson())
  }
  /// 根据 相册标识符 获取相册
  /// - Parameters:
  ///   - call: call
  ///   - result: result
  public func getAlbum(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
    guard let arguments = call.arguments as? [String:Any] else {
      let error = FlutterError(code: "-10000", message: "缺少相册标识符", details: nil)
      result(error)
      return
    }
    guard let localIdentifier = arguments["localIdentifier"] as? String else {
      let error = FlutterError(code: "-10000", message: "缺少相册标识符", details: nil)
      result(error)
      return
    }
    guard let album = AIPhotoTool.fetchAlbum(localIdentifier) else {
      let error = FlutterError(code: "-10000", message: "相册不存在", details: nil)
      result(error)
      return
    }
    result(album.toAifJson())
  }
  
  public func getAllAssets(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
    let assetResult = AIPhotoTool.fetchAllAssets()
    result(assetResult.toAifJson())
  }
  
  public func getAlbumAssets(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
    guard let arguments = call.arguments as? [String:Any] else {
      let error = FlutterError(code: "-10000", message: "相册不存在", details: nil)
      result(error)
      return
    }
    guard let localIdentifier = arguments["localIdentifier"] as? String else {
      let error = FlutterError(code: "-10000", message: "相册不存在", details: nil)
      result(error)
      return
    }
    guard let album = AIPhotoTool.fetchAlbum(localIdentifier) else {
      let error = FlutterError(code: "-10000", message: "相册不存在", details: nil)
      result(error)
      return
    }
    let assets = AIPhotoTool.fetchAssets(album)
    result(assets.toAifJson())
  }
  
  public func getAsset(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
    guard let arguments = call.arguments as? [String:Any] else {
      let error = FlutterError(code: "-10000", message: "缺少相册标识符", details: nil)
      result(error)
      return
    }
    guard let localIdentifier = arguments["localIdentifier"] as? String else {
      let error = FlutterError(code: "-10000", message: "缺少相册标识符", details: nil)
      result(error)
      return
    }
    guard let asset = AIPhotoTool.fetchAsset(localIdentifier) else {
      let error = FlutterError(code: "-10000", message: "资源不存在", details: nil)
      result(error)
      return
    }
    result(asset.toAifJson())
  }
  
  public func getAssetPath(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
    guard let arguments = call.arguments as? [String:Any] else {
      let error = FlutterError(code: "-10000", message: "缺少相册标识符", details: nil)
      result(error)
      return
    }
    guard let localIdentifier = arguments["localIdentifier"] as? String else {
      let error = FlutterError(code: "-10000", message: "缺少相册标识符", details: nil)
      result(error)
      return
    }
    guard let asset = AIPhotoTool.fetchAsset(localIdentifier) else {
      let error = FlutterError(code: "-10000", message: "资源不存在", details: nil)
      result(error)
      return
    }
    asset.requestPath { (path) in
      guard let tempPath = path else {
        let error = FlutterError(code: "-10000", message: "路径获取失败", details: nil)
        result(error)
        return
      }
      result(tempPath)
    }
  }
  
  public func getAssetThumb(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
    guard let arguments = call.arguments as? [String:Any] else {
      let error = FlutterError(code: "-10000", message: "缺少相册标识符", details: nil)
      result(error)
      return
    }
    guard let localIdentifier = arguments["localIdentifier"] as? String else {
      let error = FlutterError(code: "-10000", message: "缺少相册标识符", details: nil)
      result(error)
      return
    }
    guard let asset = AIPhotoTool.fetchAsset(localIdentifier) else {
      let error = FlutterError(code: "-10000", message: "资源不存在", details: nil)
      result(error)
      return
    }
    asset.requestThumbPath { (thumbPath) in
      guard let tempPath = thumbPath else {
        let error = FlutterError(code: "-10000", message: "路径获取失败", details: nil)
        result(error)
        return
      }
      result(tempPath)
    }
  }
}
